import {setCache, getCache} from '../utils/cache'

import {GET_USER_TOKEN, GEt_USER_TOKEN_FAIL} from '../constants/user'
// 9d4755e6-c75e-4e66-956b-ab37d1c3a149
const cacheKey = 'cnode-user-key'
const userCache = getCache(cacheKey) ? getCache(cacheKey) : {}

const initialState = {...userCache}



export default function userReducer(state = initialState, action){
  switch(action.type){
    case GET_USER_TOKEN:
      let successState = {...state, ...action}
      setCache(cacheKey, successState)
      return successState
    case GEt_USER_TOKEN_FAIL:
      let failState =  {...state, accesstoken: action.accesstoken, loginname: null}
      setCache(cacheKey, failState)
      return failState
    default:
      return state
  }
}